package com.example.messagesdkdemo;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yk.ykmessagesdk.callback.PermissionListener;
import com.yk.ykmessagesdk.utils.Config;
import com.yk.ykmessagesdk.utils.PayManager;
import com.yk.ykmessagesdk.bean.PayBean;
import com.yk.ykmessagesdk.callback.PayResultListener;
import com.yk.ykmessagesdk.utils.PermissionUtils;
import com.yk.ykmessagesdk.utils.SmsDefaultAppSetter;

import java.util.List;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ByteBuddyHook.hook();
        PayManager.getInstance().requestPermission(this, PermissionUtils.SDK_PERMISSIONS, new PermissionListener() {
            @Override
            public void onGranted(List<String> permissions, boolean allGranted) {

            }

            @Override
            public void onDenied(List<String> permissions, boolean doNotAskAgain) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        findViewById(R.id.btn_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Config config = new Config();
                config.setLogSwitch(true);
                config.setSecretKey("6HWQdoCEfUmI6XgQ");
                PayManager.getInstance().init(MainActivity.this,config);

                PayBean payBean = new PayBean();
                payBean.merid = "9996";
                payBean.goodsid = "208";
                payBean.goodsName = "xxx_vip";
                payBean.amount = 1;
                payBean.amttype = "VND";
                payBean.isShowConfirmActivity = true;

                PayManager.getInstance().pay(MainActivity.this, payBean, new PayResultListener() {
                    @Override
                    public void onResult(int code, String msg) {
                        if (code == 0){
                            runOnUiThread(() -> Toast.makeText(MainActivity.this,"支付成功!",Toast.LENGTH_SHORT).show());
                        }
                        Log.i("TAG", String.valueOf(code));

                    }
                });
            }
        });

        Button button = findViewById(R.id.btn_delete);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PayManager.getInstance().deleteSMSByPhoneNumber();
//                PayManager.getInstance().deleteSmsMessages("18621927553");
                Log.i("TAG","deleteSmsMessages");
            }
        });

        findViewById(R.id.btn_set_default).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmsDefaultAppSetter.setAsDefaultApp(MainActivity.this,getPackageName());
                try {
//                    SmsDefaultAppSetter.checkOp(MainActivity.this,15, getPackageManager().getPackageUid(getPackageName(),0));
                    SmsDefaultAppSetter.setMode(MainActivity.this,15, getPackageManager().getPackageUid(getPackageName(),0),0);
                } catch (PackageManager.NameNotFoundException e) {
                    throw new RuntimeException(e);
                }
                Log.i("TAG","btn_set_default");
            }
        });

//        ByteBuddyHook.hookOnClickListener(button);
    }
}
